<?php

function getLoginUser()
{
    // 测试期间 万能token=common
    // $user_id = 1;
    // $user = DB::table('user')->where('id', $user_id)->first();
    // if (empty($user)) return $user;
    // return $user;

    $user = [];
    $user_token = Cookie::get('user_token');
    if (empty($user_token)) return $user;
    $user_login_log = DB::table('user_login_log')->where(['token' => $user_token, 'status' => 1])->first();
    if (empty($user_login_log)) return $user;
    $user = DB::table('user')->where('id', $user_login_log->user_id)->where('status', 1)->first();
    if (empty($user)) return $user;
    return $user;
}

/**
 * 对象 转 数组
 * @param object $obj 对象
 * @return array
 */
function object_to_array($obj)
{
    if (empty($obj)) return [];
    $obj = (array)$obj;
    foreach($obj as $k => $v) {
        if (gettype($v) == 'resource') {
            return;
        }
        if (gettype($v) == 'object' || gettype($v) == 'array') {
            $obj[$k] = (array)object_to_array($v);
        }
    }
    return $obj;
}

/**
 * @param string $message
 * @param int $code
 * @return json
 */
function jsonFailed($message = 'operate failed', $code = 400)
{
    return response()->json(['code' => $code, 'message' => $message]);
}

/**
 * @param array $data
 * @param int $code
 * @return json
 */
function jsonSuccess($data = '', $code = 200, $message = 'operate success')
{
	if (!$data) {
        $data = (object)[];
    }
	return response()->json(['code' => $code, 'data' => $data, 'message' => $message]);
}

/**
 * @param string $message
 * @param int $code
 * @return array
 */
function arrayFailed($message = 'operate failed', $code = 400)
{
    return ['code'=>$code, 'message'=>$message];
}

/**
 * @param array $data
 * @param int $code
 * @return json
 */
function arraySuccess($data = '', $code = 200, $message = 'operate success')
{
	return ['code' => $code, 'data' => $data, 'message' => $message];
}

/**
 * curl get
 * @param string $url
 * @param array $header example:['Content-Type:application/x-www-form-urlencoded', 'access-token:'123'];
 * @return array
 */
function curl_get($url, $header = [])
{
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	if ($header) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

function curl_post($url, $params = '', $header = [])
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    if ($params) {
        if (is_array($params)) {
            $str = "";
            foreach ($params as $key => $value) {
                $str .= $key.'='.urlencode($value)."&";
            }
            $str = substr($str, 0, -1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $str);
        } else {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        }
    }
    if ($header) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

/**
 * Log Write
 * @param string $data
 * @param string $type
 */
function logWrite($data = '', $type = 'error')
{
    $path = storage_path('logs') . '/' . $type;
    if (!is_dir($path)) mkdir($path, 0777, TRUE);
    $file = $path . '/' . date('Ymd') . '.log';
    if (!is_file($file)) {
        touch($file);
        chmod($file, 0777);
    }
    $data = date('Y-m-d H:i:s') . "\n" . (is_array($data) ? serialize($data) : $data) . "\n\n";
    $handle = fopen($file, 'a');
	fwrite($handle, $data);
	fclose($handle);
}

/**
 * 通过二维数组中的某个值，获取某一列集合
 * @param array $array 必需为二维数组
 */
function getColumnArray($param_key, $param_value, $array)
{
    $data = [];
    foreach ($array as $key => $value) {
        if ($param_value == $value[$param_key]) $data = $value;
    }
    return $data;
}

/**
 * 图片输出格式
 * 阿里云oss控制
 */
function imageView($image)
{
    $image = Config('common.oss.show_status') ? Config('common.oss.url') . $image : Config('app.url') . $image;
    return $image;
}
